package com.zhuss.team.processor;

import com.zhuss.bean.bo.team.publish.DetailPublishUserBo;
import com.zhuss.bean.bo.team.publish.SearchPublishUserBo;
import com.zhuss.bean.unit.team.AnswerBean;
import com.zhuss.bean.unit.team.QuestionBean;
import com.zhuss.bean.vo.team.invest.DetailInvestVo;
import com.zhuss.bean.vo.team.publish.DetailPublishUserVo;
import com.zhuss.bean.vo.team.publish.DetailPublishVo;
import com.zhuss.bean.vo.team.publish.ListAnswerVo;
import com.zhuss.bean.vo.team.publish.ListPublishUserVo;
import com.zhuss.common.PageVo;
import com.zhuss.enums.PublishTimeType;
import com.zhuss.exception.WpsException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.team.db.dao.ext.InvestPublishUserDao;
import com.zhuss.team.db.entity.InvestAnswer;
import com.zhuss.team.db.entity.Question;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @Author: zhushanshuo
 * @Date: 2020/3/26 13:25
 */
@Component("publishUserRead")
public class PublishUserRead {

    @Autowired
    private InvestPublishUserDao publishUserDao;

    @Autowired
    private PublishRead publishRead;

    @Autowired
    private PublishUserRead publishUserRead;

    @Autowired
    private InvestRead investRead;

    @Autowired
    private InvestQuestionRead investQuestionRead;

    @Autowired
    private QuestionRead questionRead;

    @Autowired
    private AnswerRead answerRead;

    /**
     * 用户列表
     * @param bo
     */
    public Object list(SearchPublishUserBo bo) throws WpsException {
        PageVo<ListPublishUserVo> result = new PageVo<ListPublishUserVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        result.setItems(transfer(publishUserDao.list(bo)));
        result.setTotal(publishUserDao.countList(bo));
        return result;
    }

    private List<ListPublishUserVo> transfer(List<ListPublishUserVo> list) throws WpsException {
        Date now = new Date();
        for (ListPublishUserVo item : list) {
            DetailPublishVo vo = (DetailPublishVo) publishRead.detail(item.getPublishId());
            Date startTime = item.getStartTime();
            Date endTime = item.getEndTime();
            if (startTime.getTime()<now.getTime() && endTime.getTime()>now.getTime()){
                item.setTimeStatus(PublishTimeType.可答题.key());
            }else{
                item.setTimeStatus(PublishTimeType.不可答题.key());
            }
            item.setInvestName(vo.getInvestName());
        }
        return list;
    }

    /**
     * 详情
     * @param publishUserId
     * @return
     * @throws WpsException
     */
    public Object detail(Long publishUserId) throws WpsException {
        Object result = publishUserDao.detail(publishUserId);

        if (result == null) {
            throw new WpsException(ExceptionCode.数据不存在);
        }
        return result;
    }

    /**
     * 获取问卷问题详情
     * @param bo
     * @return
     * @throws WpsException
     */
    public Object answerList(DetailPublishUserBo bo) throws WpsException {
        ListAnswerVo result = new ListAnswerVo();
        AnswerBean answerBean = new AnswerBean();
        DetailPublishUserVo publishUserVo = (DetailPublishUserVo)publishUserRead.detail(bo.getPublishUserId());
        DetailInvestVo investVo = (DetailInvestVo)investRead.detail(publishUserVo.getInvestId());
        answerBean.setInvestId(investVo.getInvestId());
        answerBean.setInvestName(investVo.getInvestName());
        answerBean.setPublishId(publishUserVo.getPublishId());
        answerBean.setPublishUserId(bo.getPublishUserId());
        List<Long> questionIdList = (List<Long>) investQuestionRead.listQuestionId(publishUserVo.getInvestId());
        List<Question> questionList = (List<Question>)questionRead.listQuestion(questionIdList);
        answerBean.setQuestionList(transferList(questionList, bo.getPublishUserId()));
        result.setAnswer(answerBean);
        return result;
    }

    private List<QuestionBean> transferList(List<Question> list, Long publishUserId) throws WpsException {
        List<InvestAnswer> listVo = (List<InvestAnswer>)answerRead.answerList(publishUserId);
        Map<Long, String> answerMap = new HashMap<>();
        for (InvestAnswer item : listVo) {
            Long questionId = item.getQuestionId();
            String choose = item.getChoose();
            answerMap.put(questionId, choose);
        }
        List<QuestionBean> result = new ArrayList<>();
        for (Question item : list) {
            QuestionBean question = new QuestionBean();
            BeanUtils.copyProperties(item, question);
            question.setAnswer(answerMap.get(item.getQuestionId()));
            result.add(question);
        }
        return result;
    }

}
